#!/bin/bash

#=======================================================================
# multiapt
# File ID: ffa9a0ac-28af-11e4-9136-c80aa9e67bbd
# [Description]
# License: GNU General Public License version 2 or later.
#=======================================================================

progname=multiapt

cmd_str=install
test "$1" = "-r" && { cmd_str=remove; shift; }
test "$1" = "-u" && { cmd_str=upgrade; shift; }

if test -e "/gitrepos.txt"; then
    cd /
    git update-dirs $(cat gitrepos.txt) -q -e "git wait-until-clean"
fi

for f in "$@"; do
    cd /etc || { echo $progname: Cannot chdir to /etc >&2; exit 1; }
    echo
    if test "$cmd_str" = "remove"; then
        echo apt-get remove $f
        apt-get remove $f
    else
        echo apt-get install $f
        apt-get install "$f"
    fi
    if test "$cmd_str" = "install"; then
        ciall -d -y apt-get install $f
    elif test "$cmd_str" = "upgrade"; then
        ciall -d -y upgrade package $f
    elif test "$cmd_str" = "remove"; then
        ciall -d -y apt-get remove $f
    else
        echo $progname: invalid \$cmd_str value: \"$cmd_str\" >&2
        exit 1
    fi
    if test -e "/gitrepos.txt"; then
        cd /
        if test "$cmd_str" = "install"; then
            git update-dirs $(cat gitrepos.txt) -e "ciall -y apt-get install $f"
        elif test "$cmd_str" = "upgrade"; then
            git update-dirs $(cat gitrepos.txt) -e "ciall -y upgrade package $f"
        elif test "$cmd_str" = "remove"; then
            git update-dirs $(cat gitrepos.txt) -e "ciall -y apt-get remove $f"
        fi
    fi
done
